package com.service.imp;

import com.common.Base;
import com.common.Result;
import com.entity.User;
import com.repository.UserRepository;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.Optional;

/**
 * @Author：Luohuiyong
 * @Description:
 * @Date: Created in 11:06 2019/1/9
 */
public class UserServiceImp implements UserService {

    @Autowired
    private UserRepository userRepository;

    /**
     * 通过id查找
     * @param id
     * @return
     */
    @Transactional
    public User findOne(String id){
        Optional<User> optionalUser = userRepository.findById(id);
        return optionalUser.get();
    }

    /**
     * 新增一个实体
     * @param user
     * @return
     */
    public Result create(User user){
        user.setCreatedAt(new Date());
        userRepository.save(user);

        return Base.success();
    }
}
